<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流水单号" prop="serialNum">
        <el-input
          v-model="queryParams.serialNum"
          placeholder="请输入流水单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="applyUserName">
        <el-input
          v-model="queryParams.applyUserName"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:oaDimission:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:oaDimission:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:oaDimission:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:oaDimission:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="oaDimissionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水单号" align="center" prop="serialNum" />
      <el-table-column label="申请人" align="center" prop="applyUserName" />
      <el-table-column label="公司" align="center" prop="company" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="岗位" align="center" prop="post" />
      <el-table-column label="类型" align="center" prop="type" />
<!--      <el-table-column label="性别" align="center" prop="sex" />-->
      <el-table-column label="入职日期" align="center" prop="entryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离职申请时间" align="center" prop="applyDimissionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyDimissionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拟离职日期" align="center" prop="dimissionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dimissionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离职原因" align="center" prop="dimissionReason" />
      <el-table-column label="业务状态" align="center" prop="businessStatus" />
<!--      <el-table-column label="是否使用模板" align="center" prop="typeStatus" />-->
<!--      <el-table-column label="审批人姓名(多个）" align="center" prop="approverName" />-->
<!--      <el-table-column label="抄送人姓名(多个）" align="center" prop="copyerName" />-->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:oaDimission:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:oaDimission:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改离职申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-form-item label="申请人" prop="applyUserName">
          <el-input v-model="form.applyUserName" placeholder="请输入申请人" :disabled="true"/>
        </el-form-item>

        <el-form-item label="公司" prop="company">
<!--          <el-input v-model="form.company" placeholder="请输入公司" />-->
          <el-cascader
            style="width: 100%"
            v-model="value"
            :options="deptlist"
            :props="optionsProp"
            size="medium "
            @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="岗位" prop="post">
<!--          <el-input style="width: 250px" v-model="form.post" placeholder="请输入岗位" />-->
          <el-select
            @change="changepostinfo"
            v-model="form.post"
            style="width: 100%"
            :multiple="false"
            filterable
            remote
            reserve-keyword
            placeholder="请填写岗位"
            :remote-method="getpostinfolist"
            :loading="loading">
            <el-option
              v-for="item in postinfolist"
              :key="item.id"
              :label="item.postName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择" style="width: 100%">
            <el-option
              style="width:100%"
              v-for="item in typeoptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入职日期" prop="entryDate" >
          <el-date-picker clearable
           style="width: 100%"
            v-model="form.entryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入职日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="离职申请时间" prop="applyDimissionDate">
          <el-date-picker clearable
            v-model="form.applyDimissionDate"
           style="width: 100%"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择离职申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="拟离职日期" prop="dimissionDate">
          <el-date-picker clearable
            v-model="form.dimissionDate"
            type="date"
            style="width: 100%"
            value-format="yyyy-MM-dd"
            placeholder="请选择拟离职日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="离职原因" prop="dimissionReason">
          <el-input v-model="form.dimissionReason" placeholder="请输入离职原因" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input  v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否使用流程模板" prop="typeStatus">
          <el-select v-model="form.typeStatus" placeholder="请选择"   @change="changeValue">
            <el-option
              style="width: 100%"
              v-for="item in ynOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <!--自定义-->
        <div v-if="showApproval3">
          <div style="margin-left: 30px">
            <span>请选择审批人</span>
            <el-select @change="changeuseroptions" v-model="value1" multiple placeholder="请选择">
              <el-option
                style="width: 100%"
                v-for="item in userlistoptions"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId">
              </el-option>
            </el-select>
<!--            <el-input v-model="approver.userName" style="width: 300px">-->
<!--              <template slot="append">-->
<!--                <el-button type="primary"  @click="openUser(1)">-->
<!--                  选择审批人-->
<!--                </el-button>-->
<!--              </template>-->
<!--            </el-input>-->
          </div>
          <div style="margin-left: 30px;margin-top: 10px">
            <span style="margin-right: 5px">请选择抄送人</span>
            <el-input v-model="copyer.userName" style="width: 300px">
              <template slot="append">
                <el-button type="primary"  @click="openUser(2)">
                  选择抄送人
                </el-button>
              </template>
            </el-input>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <import-user ref="user" @showUser="showUser"></import-user>
  </div>
</template>

<script>
import { listOaDimission, getOaDimission, delOaDimission, addOaDimission, updateOaDimission } from "@/api/module/oaDimission";
import {deptTreeSelect} from "@/api/system/user";
import {listPostInfo} from "@/api/module/postInfo";
import {getDicts} from "@/api/system/dict/data";
import importUser from '@/views/dialog/user.vue';
import {getUserProfile,getuserList,getuserrank} from '@/api/system/user';

export default {
  name: "OaDimission",
  components:{
    importUser
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 离职申请表格数据
      oaDimissionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNum: null,
        company: null,
        deptName: null,
        deptId: null,
        post: null,
        postId: null,
        type: null,
        sex: null,
        entryDate: null,
        applyDimissionDate: null,
        dimissionDate: null,
        dimissionReason: null,
        businessStatus: null,
        applyUserId: null,
        applyUserName: null,
        typeStatus: null,
        approver: null,
        approverName: null,
        copyerId: null,
        copyerName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      deptlist:[],
      value:[],
      optionsProp:{value:'id',label:'label',children:'children'},
      ynOptions:[],//是否
      postinfolist:[],
      typeoptions:[],
      showApproval3:false,
      approver:{},//自定义审批人
      copyer:{},
      value1:[],
      userlistoptions:[]
    };
  },
  created() {
    this.getList();
    this.getdeptlist()
    this.getpostinfolist()//岗位
    this.getYesNo()
    this.gettypeoptions()
    this.getrankuser()
  },

  methods: {
    // 查询主管级别用户
    getrankuser(){
      getuserrank({rank:"主管",pageSize:1000}).then(res=>{
        this.userlistoptions=res.rows
      })
    },
    changeuseroptions(val){
      this.form.approverName=null
      val.forEach(ids=>{
        this.userlistoptions.forEach(item=>{
          if(ids==item.userId){
            if(this.form.approverName==null){
              this.form.approverName=item.userName
            }else {
              this.form.approverName= this.form.approverName+','+item.userName
            }
          }
        })
      })
    },
    //获取部门列表
    async getdeptlist(){
      let res=await deptTreeSelect()
      this.deptlist=res.data
    },
    handleChange(value){
      console.log(value)

      /*  this.form.company*/
      this.form.deptId=value[2]
      this.getlcompany(this.deptlist,value[1],this.form.company)
      this.getdeptlabel(this.deptlist,value[2],this.form.company)
      console.log(this.form,909090)
    },
    //遍历数组
    getlcompany(arr,value,label){
      var newlabel=''
      for(var i=0;i<arr.length;i++){
        if(arr[i].id==value){
          newlabel=arr[i].label
          this.form.company=arr[i].label

        }

        if(arr[i].children){
          this.getlcompany(arr[i].children,value,label)
        }

      }


    },
    getdeptlabel(arr,value,label){
      arr.forEach(item=>{
        if(item.id==value){
          label=item.label
          this.form.deptName=item.label
          return
        }

        if(item.children){
          this.getdeptlabel(item.children,value,label)
        }
      })
    },
    //获取岗位
    getpostinfolist(query){
      listPostInfo({postName:query}).then(res=>{
        this.postinfolist=res.rows
      })

    },
    changepostinfo(val){
      this.form.postId=val
      this.postinfolist.forEach(item=>{
        if(item.id==val){
          this.form.post=item.postName
        }
      })

    },
    //是或否
    getYesNo(){
      getDicts('yesorno').then(res=>{
        this.ynOptions=res.data
      })
    },
    //离职类型
    gettypeoptions(){
      getDicts('dimisson').then(res=>{
        this.typeoptions=res.data
      })
    },
    /** 查询离职申请列表 */
    getList() {
      this.loading = true;
      listOaDimission(this.queryParams).then(response => {
        this.oaDimissionList = response.rows;
        let arry=this.oaDimissionList
        arry.forEach(item=>{
          item.type=this.getValue(this.typeoptions,item.type)
        })
        this.oaDimissionList=arry
        this.total = response.total;
        this.loading = false;
      });
    },
    changeValue(val){
      if(val=='0') {
        //进度条展示
        this.showApproval3 = false
        this.showApproval2 = false
      }else{
        //  自定义类型
        this.showApproval2=false
        this.showApproval3=true
      }
    },
    getValue(arr, item) {
      for (let i = 0; i < arr.length; i++) {
        if (item === arr[i].dictValue) {
          return arr[i].dictLabel
        }
      }
    },
    //打开用户
    openUser(val){
      this.$refs.user.show(val,1)
    },

    showUser(data){

      if(data.type=='0'){
        //自定义类型赋值
        this.form.userId=data.userId
        this.form.userName=data.userName
      }
      if(data.type=='1'){
        this.approver=data

        //自定义类型赋值
        this.form.approver=data.userId
        this.form.approverName=data.userName
      }
      if(data.type=='2'){
        this.copyer=data
        this.form.copyerId=data.userId
        this.form.copyerName=data.userName
      }


    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.value=[]
      this.form = {
        id: null,
        serialNum: null,
        company: null,
        deptName: null,
        deptId: null,
        post: null,
        postId: null,
        type: null,
        sex: null,
        entryDate: null,
        applyDimissionDate: null,
        dimissionDate: null,
        dimissionReason: null,
        businessStatus: null,
        applyUserId: null,
        applyUserName: null,
        typeStatus: null,
        approver: null,
        approverName: null,
        copyerId: null,
        copyerName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.applyUserId=this.$store.getters.id
      this.form.applyUserName=this.$store.getters.name
      this.open = true;
      this.title = "添加离职申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOaDimission(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改离职申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.approver=this.value1.join(',')
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOaDimission(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOaDimission(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除离职申请编号为"' + ids + '"的数据项？').then(function() {
        return delOaDimission(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/oaDimission/export', {
        ...this.queryParams
      }, `oaDimission_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
